package com.newa.listeners;

import java.util.ArrayList;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.newa.beans.Work;
import com.newa.dao.DAOWorks;
import com.newa.utils.HibUtil;

/**
 * Application Lifecycle Listener implementation class Listener
 *
 */
public class Listener implements ServletContextListener {
	private Logger LOG = Logger.getLogger(Listener.class);
    /**
     * Default constructor. 
     */
    public Listener() {
        // TODO Auto-generated constructor stub
    }

	/**
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent even) {
    	Session session = HibUtil.getSessionFactory().openSession();
    	DAOWorks daow = new DAOWorks(session);
    	try {
        	ArrayList<Work> list = daow.getAllWorks();
        	even.getServletContext().setAttribute("worksMain", list);
		} catch (Exception e) {
			LOG.warn(e);
			throw new RuntimeException(e);
		} finally{
			daow.closeSession().close();
		}
    	
    }

	/**
     * @see ServletContextListener#contextDestroyed(ServletContextEvent)
     */
    public void contextDestroyed(ServletContextEvent cont) {
        // TODO Auto-generated method stub
    }
	
}
